package epam.generics.function;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import epam.generics.fruit.*;

/**
 * @author Iurii Miedviediev
 * @version 1.0 Build 28.04.2014
 */
public class FruitGenerics {

    private FruitGenerics() {
    }

    /**
     * Copy from array to Collection
     * @param <T> any type
     * @param a array of type T
     * @param c Collection of type T
     */
    public static <T> void fromArrayToCollection(T[] a, Collection<T> c) {
        for(T element : a) {
            c.add(element);
        }
    }

    /**
     * Copy all elements from one collection into the other
     * @param <T> Any object of class E or its subclass
     * @param collection source collection
     * @param out destination collection
     */
    public static <T> void copyAll(Collection<? extends T> collection, Collection<T> out) {
        for(T element : collection) {
            out.add(element);
        }
    }

    /**
     * Filter collection from all elements, less then given criteria
     * @param <T> Any Type that implements Comparable interface
     * @param source source collection
     * @param criteria criteria to filter source collection
     * @return Collection with filtered results
     */
    public static <T extends Comparable<T>> Collection filterCollection(Collection<T> source, T criteria) {
        List<T> result = new ArrayList();
        for(T element : source) {
            if(criteria.compareTo(element) > 0) {
                result.add(element);
            }
        }
        return result;
    }
}